package com.xh.one2one.lesson.service.impl;

import com.xh.one2one.lesson.bean.request.LessonTimesAuditRequest;
import com.xh.one2one.lesson.model.LessonTimesAudit;
import com.xh.one2one.lesson.dao.LessonTimesAuditMapper;
import com.xh.one2one.lesson.model.LessonTimesAuditComment;
import com.xh.one2one.lesson.service.ILessonTimesAuditCommentService;
import com.xh.one2one.lesson.service.ILessonTimesAuditService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
/**
 * <p>
 * 备课中教学计划的审核 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-04-22
 */
@Service
public class LessonTimesAuditServiceImpl extends BaseServiceImpl<LessonTimesAuditMapper, LessonTimesAudit> implements ILessonTimesAuditService {

    @Autowired
    private ILessonTimesAuditCommentService lessonTimesAuditCommentService;

    @Override
    public XhPage queryLessonTimesAuditPage(XhPage page, LessonTimesAuditRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonTimesAudit>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryLessonTimesAudit(LessonTimesAuditRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonTimesAudit>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(LessonTimesAuditRequest request) {
        LessonTimesAudit lessonTimesAudit = new LessonTimesAudit();
        BeanUtils.copyProperties(request,lessonTimesAudit);
        this.insert(lessonTimesAudit);
        if(!StringUtils.isEmpty(request.getComment())){
            LessonTimesAuditComment  comment = new LessonTimesAuditComment();
            comment.setLessonId(request.getLessonId());
            comment.setVersion(request.getVersion());
            comment.setComment(request.getComment());
            lessonTimesAuditCommentService.insert(comment);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByLessonId(LessonTimesAuditRequest bean) {
        QueryWrapper qw=new QueryWrapper<LessonTimesAudit>();
        qw.eq("lesson_id",bean.getLessonId());
        List<LessonTimesAudit> list = this.dao.selectList(qw);
        if (!CollectionUtils.isEmpty(list)){
            LessonTimesAudit lessonTimesAudit = list.get(0);
            lessonTimesAudit.setStatus(0);
            lessonTimesAudit.setVersion(bean.getVersion());
            this.update(lessonTimesAudit);
        }
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(LessonTimesAuditRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(!StringUtils.isEmpty(bean.getLessonId())){
            qw.eq("lesson_id",bean.getLessonId());
        }
        if(null != bean.getVersion()){
            qw.eq("version",bean.getVersion());
        }
        return qw;
    }
}
